package com.freeter.modules.gjs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.freeter.modules.gjs.entity.UserRoles;
import com.freeter.modules.gjs.mapper.UserRolesMapper;
import com.freeter.modules.gjs.service.UserRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserRolesServiceImpl implements UserRolesService {
    @Autowired
    private UserRolesMapper userRolesMapper;

    @Override
    public List<UserRoles> getAllUserRoles() {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }

    @Override
    public UserRoles getUserRolesById(Long id) {
        return userRolesMapper.selectById(id);
    }

    @Override
    public List<UserRoles> getUserRolesByUserId(Long userId) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }

    @Override
    public List<UserRoles> getUserRolesByRoleCode(String roleCode) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("role_code", roleCode).eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }

    @Override
    public List<UserRoles> getUserRolesByRoleName(String roleName) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("role_name", roleName).eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }

    @Override
    public UserRoles getUserRolesByUserAndRole(Long userId, String roleCode) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("role_code", roleCode).eq("is_deleted", 0);
        List<UserRoles> list = userRolesMapper.selectList(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<UserRoles> getUserRolesByStatus(String status) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("status", status).eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }

    @Override
    public UserRoles createUserRoles(UserRoles userRoles) {
        userRolesMapper.insert(userRoles);
        return userRoles;
    }

    @Override
    public UserRoles updateUserRoles(Long id, UserRoles userRoles) {
        userRoles.setId(id);
        userRolesMapper.updateById(userRoles);
        return userRoles;
    }

    @Override
    public boolean deleteUserRoles(Long id) {
        return userRolesMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteUserRolesByUserAndRole(Long userId, String roleCode) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId).eq("role_code", roleCode);
        return userRolesMapper.delete(wrapper) > 0;
    }

    @Override
    public List<UserRoles> getUserRolesByCondition(String name, Integer state) {
        Wrapper<UserRoles> wrapper = new EntityWrapper<>();
        if (name != null) wrapper.eq("name", name);
        if (state != null) wrapper.eq("state", state);
        wrapper.eq("is_deleted", 0);
        return userRolesMapper.selectList(wrapper);
    }
} 